/*
 * @Author: juzi.liu
 * @Date: 2021-04-09 09:22:43
 * @Last Modified by: juzi.liu
 * @Last Modified time: 2021-04-09 09:45:51
 */

class flylinexml {
  /**
   * 添加飞行的路线节点
   * @param {*} route ={idnex:0,speed:0, ~~~}
   */
  createXMLflyLine(route) {
    const parser = new DOMParser()
    window.parser = parser
    const xmlDoc = parser.parseFromString(
      '<cusxmlRoute></cusxmlRoute>',
      'text/xml'
    )
    window.xmlDoc = xmlDoc
    const sceneRouteNode = this.createSceneRouteNode(xmlDoc)

    // 创建路线
    const routeNode = this.createRouteNode(xmlDoc, route)
    const routeStyleNode = this.createRouteStyleNode(xmlDoc)
    routeNode.appendChild(routeStyleNode)
    // 开始添加站点
    const _that = this
    const stops = route.routeStops
    if (stops.length < 2) {
      console.warn('节点数小于2')
      return
    }
    for (let index = 0; index < stops.length; index++) {
      const stop = stops[index]
      const camerainfo = {
        longitude: stop.point[0],
        latitude: stop.point[1],
        altitude: stop.point[2] + 100,
        heading: stop.heading * (180 / Math.PI),
        tilt: stop.tilt * (180 / Math.PI) + 90
      }

      const stopnode = _that.createStopNode(
        xmlDoc,
        camerainfo,
        stop,
        index
      )
      routeNode.appendChild(stopnode)
    }
    sceneRouteNode.appendChild(routeNode)
    xmlDoc.children[0].appendChild(sceneRouteNode)
    const flylinexml = xmlDoc.children[0].innerHTML
    return flylinexml
  }

  createSceneRouteNode(rootXmlDoc) {
    const sceneRouteNode =
      rootXmlDoc.createElement('SceneRoute')
    sceneRouteNode.setAttribute(
      'xmlns',
      'http://www.supermap.com.cn/ugc60'
    )
    return sceneRouteNode
  }

  createRouteNode(rootXmlDoc, route) {
    const routeNode = rootXmlDoc.createElement('route')
    const attrs = {
      name: route.routeName || '飞行路线',
      speed: route.speed || '200',
      lineType: '0',
      showroutestop: 'False',
      showrouteline: 'False',
      altitudefree: 'False',
      headingfree: 'False',
      tiltfree: 'False',
      flycircle: 'False',
      alongline: route.isAlongLine || 'False'
    }
    for (const key in attrs) {
      routeNode.setAttribute(key, attrs[key])
    }
    return routeNode
  }

  createRouteStyleNode(rootXmlDoc) {
    const routeStyleNode = rootXmlDoc.createElement('style')
    const geostyle3d =
      rootXmlDoc.createElement('geostyle3d')
    const linecolor = rootXmlDoc.createElement('linecolor')
    linecolor.textContent = 'RGBA(147,112,219,255)'

    const linewidth = rootXmlDoc.createElement('linewidth')
    linewidth.textContent = 2
    const altitudeMode =
      rootXmlDoc.createElement('altitudeMode')
    altitudeMode.textContent = 'Absolute'
    const bottomAltitude = rootXmlDoc.createElement(
      'bottomAltitude'
    )
    bottomAltitude.textContent = 0.0
    geostyle3d.appendChild(linecolor)
    geostyle3d.appendChild(linewidth)
    geostyle3d.appendChild(altitudeMode)
    geostyle3d.appendChild(bottomAltitude)

    routeStyleNode.appendChild(geostyle3d)
    return routeStyleNode
  }

  createStopNode(rootXmlDoc, camerainfo, stop, index) {
    const stopNode = rootXmlDoc.createElement('routestop')
    const name = stop.stopName || 'Stop' + (index + 1)
    const attris = {
      name: name,
      speed: stop.speed,
      excluded: 'False',
      viewType: 'camera'
    }
    for (const key in attris) {
      stopNode.setAttribute(key, attris[key])
    }
    const cameranode = this.createStopCameraNode(
      rootXmlDoc,
      camerainfo
    )
    stopNode.appendChild(cameranode)
    const stylenode = this.createStopStyleNode(rootXmlDoc)
    stopNode.appendChild(stylenode)

    const settingnode = this.createStopSettingNode(
      rootXmlDoc,
      stop
    )
    stopNode.appendChild(settingnode)
    return stopNode
  }

  createStopCameraNode(rootXmlDoc, cameraObj) {
    const stopCameraNode =
      rootXmlDoc.createElement('camera')
    for (const key in cameraObj) {
      const node = rootXmlDoc.createElement(key)
      node.textContent = cameraObj[key]
      stopCameraNode.appendChild(node)
    }
    return stopCameraNode
  }

  createStopStyleNode(rootXmlDoc) {
    const nodeObj = {
      icon: '',
      markersize: 4.8,
      markericonscale: 1,
      markercolor: 'RGBA(255, 255, 255, 255)'
    }
    const stopStyleNode = rootXmlDoc.createElement('style')
    const geostyle3d =
      rootXmlDoc.createElement('geostyle3d')

    for (const key in nodeObj) {
      const node = rootXmlDoc.createElement(key)
      node.textContent = nodeObj[key]
      geostyle3d.appendChild(node)
    }
    stopStyleNode.appendChild(geostyle3d)
    return stopStyleNode
  }

  createStopSettingNode(rootXmlDoc, stop) {
    const stopSettingNode =
      rootXmlDoc.createElement('setting')
    const nodesObj = {
      turnTime: stop.surroundDuration || 1.5,
      turnSlowly: 'False',
      stopPlayMode: stop.stopPlayMode || 'StopPause',
      autoPlay: 'False',
      pauseTime: stop.waitTime || 0,
      angularSpeed: 1
    }
    for (const key in nodesObj) {
      const node = rootXmlDoc.createElement(key)
      node.textContent = nodesObj[key]
      stopSettingNode.appendChild(node)
    }
    return stopSettingNode
  }
}
export default flylinexml
